// ======================================================================
// Copyright (C) 2013 Hell-Prototypes. / www.hellprototypes.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// ======================================================================
#include "ctrl_feed_zone.h"
#include <QMessageBox>

ctrl_feed_zone::ctrl_feed_zone(int id, QWidget *parent) : QWidget(parent)
{
    if(id < 0) {
        return;
    }
    m_id = id;
    m_last_select_value = -1;
    m_part_area = 0;

    m_layout = new QHBoxLayout(this);
    m_layout->setSpacing(1);
    m_layout->setContentsMargins(0,1,0,1);

    m_set_zone_position = new QPushButton(this);
    m_set_zone_position->setText("FD:" + QString::number(m_id));
    m_set_zone_position->setMaximumWidth(40);
    m_set_zone_position->setStyleSheet("color:red");
    connect(m_set_zone_position, SIGNAL(clicked()), this, SLOT(on_pb_set_zone_position_clicked()));

    m_zone_value_list = new QComboBox(this);
    m_zone_value_list->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
    connect(m_zone_value_list, SIGNAL(currentIndexChanged(int)), this, SLOT(on_value_list_IndexChanged(int)));

    m_zone_delete = new QPushButton(this);
    m_zone_delete->setText("X");
    m_zone_delete->setMaximumWidth(16);
    connect(m_zone_delete, SIGNAL(clicked()), this, SLOT(on_pb_zone_delete_clicked()));

    m_move_arm_to_zone = new QPushButton(this);
    m_move_arm_to_zone->setText("M");
    m_move_arm_to_zone->setMaximumWidth(16);
    connect(m_move_arm_to_zone, SIGNAL(clicked()), this, SLOT(on_pb_move_arm_to_zone_clicked()));

    m_layout->addWidget(m_set_zone_position);
    m_layout->addWidget(m_zone_value_list);
    m_layout->addWidget(m_zone_delete);
    m_layout->addWidget(m_move_arm_to_zone);
    this->setLayout(m_layout);
}

void ctrl_feed_zone::set_value_list(const QStringList &list)
{
    bool refresh = false;

    if(m_zone_value_list->count() != list.size()) {
        refresh = true;
    } else {
        for(int i=0; i< list.size(); i++) {
            if(list.at(i) != m_zone_value_list->itemText(i)) {
                refresh = true;
                break;
            }
        }
    }

    if(refresh) {
        m_zone_value_list->clear();
        m_zone_value_list->addItems(list);
    }
    m_last_select_value = -1;
}

void ctrl_feed_zone::set_value_index(int index)
{
    m_last_select_value = -1;

    if(index < m_zone_value_list->count()) {
        m_zone_value_list->setCurrentIndex(index);
    }
}

void ctrl_feed_zone::on_pb_set_zone_position_clicked()
{
    QMessageBox::StandardButton ret;

    ret = QMessageBox::warning(this, tr("Remove warning"),
                               "Confirm Set Feeder Position: " + QString::number(m_id),
                                QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No);
    if(ret == QMessageBox::Yes) {
        emit feeder_zone_set_position(m_id);
    }
}

void ctrl_feed_zone::on_pb_zone_delete_clicked()
{
    QMessageBox::StandardButton ret;

    ret = QMessageBox::warning(this, tr("Remove warning"),
                               "Confirm Remove Feeder : " + QString::number(m_id),
                                QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No);
    if(ret == QMessageBox::Yes) {
        emit feeder_zone_delete(m_id);
    }
}

void ctrl_feed_zone::on_pb_move_arm_to_zone_clicked()
{
#if 0
    QMessageBox::StandardButton ret;

    ret = QMessageBox::warning(this, tr("Move warning"),
                               "Confirm Move Arm To Feeder Zone: " + QString::number(m_id),
                                QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No);
    if(ret == QMessageBox::Yes) {
        emit feeder_move_arm_to_zone(m_id);
    }
#else
    emit feeder_move_arm_to_zone(m_id);
#endif
}

void ctrl_feed_zone::on_value_list_IndexChanged(int index)
{
    if((m_last_select_value >= 0) && (!m_feeder_size.isEmpty())) {
        QMessageBox::StandardButton ret;

        ret = QMessageBox::warning(this, tr("Value change warning"),
                                   "Confirm Change Current Value: " + QString::number(m_id),
                                    QMessageBox::Yes | QMessageBox::No,
                                    QMessageBox::No);
        if(ret == QMessageBox::Yes) {
            m_last_select_value = index;
            m_feeder_size = QSize();
            m_set_zone_position->setStyleSheet("color:red");
        } else {
            m_zone_value_list->setCurrentIndex(m_last_select_value);
        }
    } else {
        m_last_select_value = index;
    }
}

void ctrl_feed_zone::set_feeder_zone_position(iPoint &pos)
{
    m_feeder_position = pos;
    //m_set_zone_position->setStyleSheet("background-color:black;font-weight: bold; color:green");
}

iPoint ctrl_feed_zone::get_feeder_zone_position()
{
    return m_feeder_position;
}

void ctrl_feed_zone::set_feeder_zone_size(QSize &size)
{
    if(size.isEmpty()) {
        return;
    }

    m_set_zone_position->setStyleSheet("font-weight: bold; color:green");
    m_feeder_size = size;
}

QSize ctrl_feed_zone::get_feeder_zone_size()
{
    return m_feeder_size;
}

QString ctrl_feed_zone::get_selected_value()
{
    return m_zone_value_list->currentText();
}

int ctrl_feed_zone::get_selected_index()
{
    return m_zone_value_list->currentIndex();
}
